package edu.fub.pub2search.db;

import java.sql.SQLException;

import javax.sql.RowSet;
import javax.sql.rowset.Predicate;

import edu.fub.pub2search.geo.GeoCalculator;
import edu.fub.pub2search.geo.GeoPosition;

public class CircularRangeFilter implements Predicate {

	/* The central position of, e.g. a person */
	private GeoPosition source;
	
	/* The size of the radius */
	private double radius;
	
	/**
	 * The constructor.
	 * 
	 * @param latitude The latitude of the central position (in micro-degrees).
	 * @param longitude The longitude of the central position (in micro-degrees).
	 * @param radius The size of the radius.
	 */
	public CircularRangeFilter(int latitude, int longitude, double radius) {
		
		this(new GeoPosition(latitude, longitude), radius);
	}
	
	/**
	 * The constructor.
	 * 
	 * @param source The central position.
	 * @param radius The size of the radius.
	 */
	public CircularRangeFilter(GeoPosition source, double radius) {
		this.source = source;
		this.radius = radius;
	}
	
	/**
	 * 
	 */
	public boolean evaluate(RowSet rs) {
		
		boolean evaluation = false;
		
		try {
			int x = rs.getInt("latitude");
			int y = rs.getInt("longitude");
			
			GeoPosition destination = new GeoPosition(x, y);
			
			// calculate the distance of each pub.
			double distance = GeoCalculator.distance(source, destination);
			
			evaluation = (distance <= radius)? true : false;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return evaluation;	
	}
	
	/**
	 * 
	 */
	public boolean evaluate(Object arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 * 
	 */
	public boolean evaluate(Object arg0, String arg1) throws SQLException {
		// TODO Auto-generated method stub
		return true;
	}

}
